﻿using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    class DistrictInsertViewModel : NotificationObject
    {
        DistrictInsert gWindow;
        DistrictViewModel gParentViewModel;

        public DistrictInsertViewModel(DistrictInsert pWindow, DistrictViewModel pParentViewModel)
        {
            gWindow = pWindow;
            gParentViewModel = pParentViewModel;

            List<WCity> list = WCity.GetAll();
            foreach(var item in list)
            {
                CityNames.Add(item.CityName.Trim());
            }
        }

        private String districtName;
        public String DistrictName
        {
            get
            {
                return districtName;
            }
            set
            {
                districtName = value;
                RaisePropertyChanged("DistrictName");
            }
        }

        private int personnelUID;
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        private List<String> cityNames = new List<String>();
        public List<String> CityNames
        {
            get
            {
                return cityNames;
            }

            set
            {
                cityNames = value;
                RaisePropertyChanged("CityNames");
            }
        }

        private String citySelected;
        public String CitySelected
        {
            get
            {
                return citySelected;
            }

            set
            {
                citySelected = value;
                RaisePropertyChanged("CitySelected");
            }
        }

        protected DelegateCommand<object> insertCommand;
        public DelegateCommand<object> InsertCommand
        {
            get
            {
                return insertCommand ?? (insertCommand = new DelegateCommand<object>(AInsertCommand, BInsertCommand));
            }
        }

        protected virtual void AInsertCommand(object obj)
        {
            List<WDistrict> list = new List<WDistrict>();
            WDistrict item = new WDistrict();
            item.DistrictName = DistrictName;
            item.PersonnelUID = PersonnelUID;
            item.CityUID = WCity.GetCityUID(CitySelected);
            list.Add(item);
            HttpResponse response = WDistrict.Insert(list);

            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BInsertCommand(object arg)
        {
            return true;
        }

        protected DelegateCommand<object> quitCommand;
        public DelegateCommand<object> QuitCommand
        {
            get
            {
                return quitCommand ?? (quitCommand = new DelegateCommand<object>(AQuitCommand, BQuitCommand));
            }
        }

        protected virtual void AQuitCommand(object obj)
        {
            gWindow.Close();
            gParentViewModel.UpdateList();
        }

        private bool BQuitCommand(object arg)
        {
            return true;
        }
    }
}
